<?php
/**
 * $Id: vote.php 26 2010-09-07 11:45:41Z shrey $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * @version 			$Rev: 26 $
 * @lastModifiedBy		$LastChangedBy: varun $
 * @lastChanged			$Date: 2010-09-14 12:44:41 +0530 (Tue, 07 Sep 2010) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

class AbstractVote extends VotesAppModel {

	var $name = 'Vote';
	var $useTable = 'votes';
	const TYPE_UP = "up";
	const  TYPE_DOWN = "down";
	const TYPE_UP_VALUE = 1;
	const TYPE_DOWN_VALUE = -1;
	protected $cacheTime = "+1 day";
	function getTopRated($type, $number = false)
	{
		$query = 'SELECT item_id, sum(score) as score FROM `votes` where type ="'.$type.'"  group by item_id order by score desc';
		
		if($number)
		{
			$query = $query.' limit '.$number;
		}
		$topRated = $this->query($query);
		
		return Set::extract($topRated, '{n}.votes.item_id');
		
	}

	function getRecentlyTopRated($type,$number=false)
	{
		$condition = 'created > SUBDATE(CURDATE(), 30) AND created <= CURDATE() AND type ="'.$type.'"';
		$list = $this->find('list',array('conditions'=>$condition));
		return $list;

	}

	function hasVoted($type,$itemId,$userId)
	{
		$condition ='item_id='.$itemId.' AND user_id = '.$userId.' AND type = "'.$type.'"';
		if($this->field('item_id',$condition))
			return true;
		else
			return false;

	}
	
	/**
	 * Returns the id of voters on a specific items
	 * @param $type
	 * @param $itemId
	 * @param $filter Conditions to filter the voters search
	 * @return unknown_type
	 */
	function _fetchVoters($finderType, $type, $itemId = null, $filter = array())
	{
		$conditions = array(
			'Vote.type' => $type
		);
		if(!is_null($itemId))
		{
			$conditions['Vote.item_id'] = $itemId;
		}
		if(!empty($filter))
		{
			$conditions = am($conditions, $filter);
		}
		$fields = array('user_id');
		$result = $this->find($finderType, compact('conditions', 'fields'));
		if(is_numeric($result))
		{
			return $result;
		}
		$userIds = Set::extract('{n}.Vote.user_id', $result);
		return $userIds;
	}
	
	function getVoters($type, $itemId = null, $filter = array())
	{
		return $this->_fetchVoters('all', $type, $itemId, $filter);
	}
	
	function getVotersCount($type, $itemId = null, $filter = array())
	{
		return $this->_fetchVoters('count', $type, $itemId, $filter);
	}
	
	function getVotesCount($modelAlias,$itemId,$voteType)
	{
		$cacheKey = $this->getCountCacheKey($modelAlias,$voteType);	
		$options = array('duration'=>$this->cacheTime);
		$voteCount = GigaModelCache::read($this->alias,$itemId,$cacheKey,$options);
		if($voteCount === false)
		{
			$value = self::TYPE_UP_VALUE;
			if($voteType == self::TYPE_DOWN)
			{
				$value = self::TYPE_DOWN_VALUE;
			}
			$conditions = array('Vote.type'=>$modelAlias,'Vote.item_id'=>$itemId,'Vote.score'=>$value);
			$voteCount = $this->find('count',array('conditions'=>$conditions));	
			GigaModelCache::write($this->alias,$itemId,$cacheKey,$voteCount,$options);
		}
		return $voteCount;
	}
	
	protected function getCountCacheKey($modelAlias,$voteType)
	{
		return $modelAlias.'_'.$voteType;
	}

	function afterSave($created)
	{
		if($created)
		{
			$type = $this->data[$this->alias]['type'];
			$itemId =  $this->data[$this->alias]['item_id'];
			GigaModelCache::delete($this->alias,$itemId,$this->getCountCacheKey($type,self::TYPE_DOWN));
			GigaModelCache::delete($this->alias,$itemId,$this->getCountCacheKey($type,self::TYPE_UP));
		}
		parent::afterSave($created);
	}
	
	function beforeSave($options)
	{
		if(isset($this->data[$this->alias]['score']))
		{
			if($this->data[$this->alias]['score']!=self::TYPE_DOWN_VALUE)
			{
				$this->data[$this->alias]['score'] = self::TYPE_UP_VALUE;
			}
		}
		return parent::beforeSave($options);
	}
	
}
?>